package com.xsl.culture.mybasevideoview.view.secondui;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.xapp.jjh.logtools.logger.LogUtil;
import com.xsl.culture.R;
import com.xsl.culture.mybasevideoview.MainActivity;
import com.xsl.culture.mybasevideoview.base.LanBaseActivity;
import com.xsl.culture.mybasevideoview.model.SubtitlesDataCoding;
import com.xsl.culture.mybasevideoview.model.SubtitlesModel;
import com.xsl.culture.mybasevideoview.utils.ScreenUtil;
import com.xsl.culture.mybasevideoview.utils.UiUtils;
import com.xsl.culture.mybasevideoview.utils.XslUtils;
import com.xsl.culture.mybasevideoview.view.subTitle.SubtitleView;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class FullVideoActivity extends LanBaseActivity {

    @BindView(R.id.video_player)
    JzvdStd videoPlayer;
    @BindView(R.id.close_btn)
    ImageView closeBtn;
    @BindView(R.id.subtitle)
    SubtitleView subtitleView;
    @BindView(R.id.video_player_layout)
    RelativeLayout layout;

    boolean isSet = false;
    String url;
    private Timer mTimer;
    private TimerTask mTimerTask;

    static {
        Jzvd.WIFI_TIP_DIALOG_SHOWED = true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        UiUtils.hideBottomUIMenu(this);
        setContentView(R.layout.activity_full_video_layout);
        XslUtils.hideStausbar(new WeakReference<>(this), true);
        ButterKnife.bind(this);

        videoPlayer.WIFI_TIP_DIALOG_SHOWED = true;
        url = getIntent().getStringExtra("url");
        flag = getIntent().getIntExtra("flag", 1);
        int position = getIntent().getIntExtra("position", 0);
        LogUtil.e("进度>>>>" + position);
//        videoPlayer.mediaInterface.seekTo(position);
//        videoPlayer.startVideo();
//        videoPlayer.setScreenFullscreen();
        videoPlayer.setUp(url, "");
//        videoPlayer.clea()
        videoPlayer.fullscreenButton.setImageResource(R.mipmap.fullscreen);
        videoPlayer.backButton.setVisibility(View.GONE);
        videoPlayer.videoCurrentTime.setVisibility(View.GONE);
        videoPlayer.backButton.setVisibility(View.GONE);
        videoPlayer.batteryLevel.setVisibility(View.GONE);
        videoPlayer.startButton.performClick();
        videoPlayer.WIFI_TIP_DIALOG_SHOWED = true;
        videoPlayer.fullscreenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.language).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toActivity(DesActivity.class);
            }
        });
        loadSubtitles();

        int w = 1920;
        int h = 1080;
        LogUtil.e("发噶" + h + "嘎嘎" + w);
        isSet = true;
        int screenH = ScreenUtil.getScreenHeight(FullVideoActivity.this);
        int nH = screenH - ScreenUtil.dp2px(70);
        int nw = nH * w / h;
        layout.setLayoutParams(new LinearLayout.LayoutParams(nw, nH));

//        videoPlayer.bottomProgressBar.add;
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                //要执行的操作
                if (null!= videoPlayer.mediaInterface){

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(videoPlayer.mediaInterface.getDuration()>0){
//                                        / videoPlayer.mediaInterface.getDuration());
                                updateSubtitle((int) videoPlayer.mediaInterface.getCurrentPosition());
                                LogUtil.e("进度>>>>>" + videoPlayer.mediaInterface.getCurrentPosition() + "市场>>>" + videoPlayer.mediaInterface.getDuration());
                            }
                        }
                    });
                }
            }
        };
        mTimer = new Timer();
        mTimer.schedule(mTimerTask, 0, 1000);
    }

    int flag = 1;//1教授介绍,2:30分钟视频

    private ArrayList<SubtitlesModel> subtitleLstCN;
    private ArrayList<SubtitlesModel> subtitleLstCA;
    private ArrayList<SubtitlesModel> subtitleLstEN;
    //字幕加载
    private void loadSubtitles() {
        try {
            AssetManager assetManager = getResources().getAssets();
            InputStream inputStreamCN;
            InputStream inputStreamCA;
            InputStream inputStreamEN;
            if (flag == 1) {
                inputStreamCN = assetManager.open("home_v_src.srt");
                inputStreamCA = assetManager.open("home_fan_src.srt");
                inputStreamEN = assetManager.open("home_en_src.srt");
            } else {
                inputStreamCN = assetManager.open("small_ch.srt");
                inputStreamCA = assetManager.open("small_tc.srt");
                inputStreamEN = assetManager.open("small_en.srt");
            }

            SubtitlesDataCoding dataCodingCN = new SubtitlesDataCoding();
            SubtitlesDataCoding dataCodingCA = new SubtitlesDataCoding();
            SubtitlesDataCoding dataCodingEN = new SubtitlesDataCoding();
            subtitleLstCN = dataCodingCN.readFileStream(inputStreamCN);
            subtitleLstEN = dataCodingEN.readFileStream(inputStreamEN);
            subtitleLstCA = dataCodingCA.readFileStream(inputStreamCA);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //關閉字幕
        subtitleView = findViewById(R.id.subtitle);
        subtitleView.setTextSize(15);
        subtitleView.setData(subtitleLstCN, SubtitleView.LANGUAGE_TYPE_CHINA);
        subtitleView.setData(subtitleLstCA, SubtitleView.LANGUAGE_TYPE_CANTONESE);
        subtitleView.setData(subtitleLstEN, SubtitleView.LANGUAGE_TYPE_ENGLISH);
//
        Log.d("Subtitle", "ca size:"+subtitleLstCN.size());
        subtitleView.setLanguage(MainActivity.curLangugue);
    }

    private void updateSubtitle(int pts) {
        if (subtitleView != null)
            subtitleView.seekTo(pts);
    }


    @Override
    protected void onPause() {
        super.onPause();
        JzvdStd.goOnPlayOnPause();
    }

    @OnClick(R.id.close_btn)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTimerTask.cancel();
        mTimer.cancel();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void updateLanguage() {
        subtitleView.setLanguage(MainActivity.curLangugue);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JzvdStd.goOnPlayOnResume();
    }
}
